home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xpt / table.c < prev    next >
C/C++ Source or Header  |  1995-05-09  |  10KB  |  352 lines

  1. /**
  2.  *
  3.  * xpt -- An X Periodic Table
  4.  *
  5.  * table.c - Create the periodic table, and handle data requests
  6.  *
  7.  * Written bu Joel P. Lord 03/05/93
  8.  *
  9.  *    This software is available for free distribution,
  10.  * under the condition that this not be removed from the
  11.  * source code.
  12.  *
  13. **/
  14.  
  15. #include "xpt.h"
  16. #include <X11/Xaw/AsciiText.h>
  17.  
  18. void init_table()
  19. {
  20.   Widget elements[104];
  21.   Widget quit_button;
  22.   int n = 0, i = 0, j = 0, k = 0, horiz_offset = 5;
  23.   Arg args[20];
  24.   char title[3];
  25.   void element_choice(), quit();
  26.   XEvent tevent;
  27.   static char *table[] = {
  28. "H                                 He",
  29. "LiBe                    B C N O F Ne",
  30. "NaMg                    AlSiP S ClAr",
  31. "K CaScTiV CrMnFeCoNiCuZnGaGeAsSeBrKr",
  32. "RbSrY ZrNbMoTcRuRhPdAgCdInSnSbTeI Xe",
  33. "CsBaLaHfTaW ReOsIrPtAuHgTlPbBiPoAtRn",
  34. "FrRaAc                              ",
  35. "                                    ",
  36. "        CePrNdPmSmEuGdTbDyHoErTmYbLu",
  37. "        ThPaU NpPuAmCmBkCfEsFmMdNoLr"
  38. };
  39.  
  40.   n = 0;
  41.   XtSetArg(args[n], XtNfromHoriz, NULL);
  42.   n++;
  43.   XtSetArg(args[n], XtNfromVert, NULL);
  44.   n++;
  45.   XtSetArg(args[n], XtNhorizDistance, 5);
  46.   n++;
  47.   XtSetArg(args[n], XtNvertDistance, 5);
  48.   n++;
  49.   XtSetArg(args[n], XtNwidth, 24);
  50.   n++;
  51.   XtSetArg(args[n], XtNheight, 32);
  52.   n++;
  53.   mbgpix = GetColors("NavyBlue", default_cmap, BP);
  54.   XtSetArg(args[n], XtNbackground, slate_grey);
  55.   n++;
  56.   XtSetArg(args[n], XtNforeground, mfgpix);
  57.   n++;
  58.   XtSetArg(args[n], XtNfont, mfontstruct);
  59.   n++;
  60.  
  61.   title[2] = 0;
  62.   for (j = 0; j < 10; j++)
  63.     {
  64.       for(i = 0; i < 36; i += 2)
  65.     {
  66.       title[0] = table[j][i];
  67.       title[1] = table[j][i+1];
  68.       if (title[0] != ' ')
  69.         {
  70.           elements[k] = XtCreateWidget(title, commandWidgetClass,
  71.                        MainW, args, n);
  72.           XtAddCallback(elements[k], XtNcallback, element_choice, k);
  73.           XtRealizeWidget(elements[k]);
  74.           XtManageChild(elements[k]);
  75.           k++;
  76.           horiz_offset += 26;
  77.           XtSetArg(args[2], XtNhorizDistance, horiz_offset);
  78.         }
  79.       else
  80.         {
  81.           horiz_offset += 26;
  82.           XtSetArg(args[2], XtNhorizDistance, horiz_offset);
  83.         }
  84.     }
  85.       horiz_offset = 0;
  86.       XtSetArg(args[0], XtNfromHoriz, NULL);
  87.       XtSetArg(args[1], XtNfromVert, elements[k-1]);
  88.       XtSetArg(args[2], XtNhorizDistance, 5);
  89.       XtSetArg(args[3], XtNvertDistance, 0);
  90.     }
  91.  
  92.   XtSetArg(args[0], XtNfromHoriz, NULL);
  93.   XtSetArg(args[1], XtNfromVert, NULL);
  94.   XtSetArg(args[2], XtNhorizDistance, 160);
  95.   XtSetArg(args[3], XtNvertDistance, 5);
  96.   XtSetArg(args[4], XtNwidth, 49);
  97.   XtSetArg(args[5], XtNheight, 32);
  98.   XtSetArg(args[6], XtNbackground, red);
  99.   XtSetArg(args[7], XtNforeground, white);
  100.   XtSetArg(args[9], XtNborderColor, red);
  101.   
  102.   quit_button = XtCreateManagedWidget("Quit", commandWidgetClass, MainW,
  103.                       args, n + 1);
  104.   XtRealizeWidget(quit_button);
  105.   XtAddCallback(quit_button, XtNcallback, quit, 0);
  106. }
  107.  
  108. static destroy_info;
  109.  
  110. void element_choice(w, element, event)
  111. Widget w;
  112. int element;
  113. XEvent *event;
  114. {
  115.   Widget element_info, ok_button, help_button;
  116.   Arg args[20];
  117.   char *data_buf;
  118.   void ok(), help();
  119.  
  120.   destroy_info = 0;
  121.  
  122.   data_buf = (char *)malloc(1000);
  123.  
  124.   element_info = XtVaCreateManagedWidget("Element Info",formWidgetClass, MainW,
  125.                        XtNfromHoriz, NULL,
  126.                        XtNfromVert, NULL,
  127.                        XtNwidth, 200,
  128.                        XtNheight, 250,
  129.                        XtNvertDistance, 60,
  130.                        XtNhorizDistance, 240,
  131.                        XtNbackground, mbgpix,
  132.                        XtNforeground, mfgpix,
  133.                        XtNfont, mfontstruct,
  134.                        XtNborderColor, slate_grey, NULL);
  135.   display_info(element_info, element);
  136.  
  137.   ok_button = XtVaCreateManagedWidget("Done", commandWidgetClass, element_info,
  138.                     XtNfromHoriz, NULL,
  139.                     XtNfromVert, NULL,
  140.                     XtNwidth, 40,
  141.                     XtNheight, 32,
  142.                     XtNvertDistance, 210,
  143.                     XtNhorizDistance, 100,
  144.                     XtNbackground, slate_grey,
  145.                     XtNforeground, mfgpix,
  146.                     XtNfont, mfontstruct,
  147.                     XtNborderColor, mbgpix, NULL);
  148.  
  149.   XtAddCallback(ok_button, XtNcallback, ok, 0);
  150.  
  151.   help_button = XtVaCreateManagedWidget("Help", commandWidgetClass,
  152.                     element_info,
  153.                     XtNfromHoriz, NULL,
  154.                     XtNfromVert, NULL,
  155.                     XtNwidth, 40,
  156.                     XtNheight, 32,
  157.                     XtNvertDistance, 210,
  158.                     XtNhorizDistance, 60,
  159.                     XtNbackground, slate_grey,
  160.                     XtNforeground, mfgpix,
  161.                     XtNfont, mfontstruct,
  162.                     XtNborderColor, mbgpix, NULL);
  163.   XtAddCallback(help_button, XtNcallback, help, 0);
  164.  
  165.   while (!destroy_info && !done)
  166.     {
  167.       XtNextEvent(&theEvent);
  168.       XtDispatchEvent(&theEvent);
  169.       if (theEvent.type == Expose)
  170.     display_info(element_info, element);
  171.     }
  172.   XtDestroyWidget(element_info);
  173.   free(data_buf);
  174. }
  175.  
  176. void ok(w, client_data, call_data)
  177. Widget w;
  178. XtPointer client_data, call_data;
  179. {
  180.   destroy_info = 1;
  181. }
  182.  
  183. void quit(w, client_data, call_data)
  184. Widget w;
  185. XtPointer client_data;
  186. XtPointer call_data;
  187. {
  188.   done = 1;
  189. }
  190.  
  191. static unsigned destroy_help = 1;
  192. static Widget help_window;
  193.  
  194. void help(w, client_data, call_data)
  195. Widget w;
  196. XtPointer client_data, call_data;
  197. {
  198.   if (!destroy_help)
  199.     {
  200.       XtDestroyWidget(help_window);
  201.       destroy_help = 1;
  202.     }
  203.   else
  204.     {
  205.       destroy_help = 0;
  206.  
  207.       help_window = XtVaCreateManagedWidget("Element Info",formWidgetClass,
  208.                         MainW,
  209.                         XtNfromHoriz, NULL,
  210.                         XtNfromVert, NULL,
  211.                         XtNwidth, 200,
  212.                         XtNheight, 250,
  213.                         XtNvertDistance, 60,
  214.                         XtNhorizDistance, 40,
  215.                         XtNbackground, mbgpix,
  216.                         XtNforeground, mfgpix,
  217.                         XtNfont, mfontstruct,
  218.                         XtNborderColor, slate_grey, NULL);
  219.       display_help(help_window);
  220.       
  221.       while (!destroy_info && !done && !destroy_help)
  222.     {
  223.       XtNextEvent(&theEvent);
  224.       XtDispatchEvent(&theEvent);
  225.       if(theEvent.type == Expose)
  226.         display_help();
  227.     }
  228.       XtDestroyWidget(help_window);
  229.       destroy_help = 1;
  230.     }
  231. }
  232.  
  233. display_help(w)
  234. Widget w;
  235. {
  236.   XDrawString(p_disp, XtWindow(w), theGC, 10, 10 + mfontheight, "Number", 6);
  237.   XDrawString(p_disp, XtWindow(w), BigGC,
  238.           100 - XTextWidth(BigFont, "Sy", 2) / 2, 110 + mfontheight,
  239.           "Sy", 2);
  240.   XDrawString(p_disp, XtWindow(w), theGC, 
  241.           190 - XTextWidth(mfontstruct, "Mass", 4),
  242.           10 + mfontheight, "Mass", 4);
  243.   XDrawString(p_disp, XtWindow(w), theGC, 10, 60 + mfontheight, "BP", 2);
  244.   XDrawString(p_disp, XtWindow(w), theGC, 10, 100 + mfontheight, "MP", 2);
  245.   XDrawString(p_disp, XtWindow(w), theGC, 10, 140 + mfontheight, "Density", 7);
  246.  
  247.   XDrawString(p_disp, XtWindow(w), theGC,
  248.           185 - XTextWidth(mfontstruct, "Atomic", 6),
  249.           55 + mfontheight, "Atomic", 6);
  250.   XDrawString(p_disp, XtWindow(w), theGC,
  251.           190 - XTextWidth(mfontstruct, "Radius", 6),
  252.           55 + 2 * mfontheight, "Radius", 6);
  253.  
  254.   XDrawString(p_disp, XtWindow(w), theGC,
  255.           185 - XTextWidth(mfontstruct, "Covalent", 8),
  256.           95 + mfontheight, "Covalent", 8);
  257.   XDrawString(p_disp, XtWindow(w), theGC,
  258.           190 - XTextWidth(mfontstruct, "Radius", 6),
  259.           95 + 2 * mfontheight, "Radius", 6);
  260.  
  261.   XDrawString(p_disp, XtWindow(w), theGC,
  262.           185 - XTextWidth(mfontstruct, "Atomic", 6),
  263.           135 + mfontheight, "Atomic", 6);
  264.   XDrawString(p_disp, XtWindow(w), theGC,
  265.           190 - XTextWidth(mfontstruct, "Volume", 6),
  266.           135 + 2 * mfontheight, "Volume", 6);
  267.  
  268.   XDrawString(p_disp, XtWindow(w), theGC,
  269.           100 - XTextWidth(mfontstruct, "Name", 4) / 2,
  270.           190 + mfontheight, "Name", 4);
  271. }
  272.  
  273. display_info(w, element)
  274. Widget w;
  275. unsigned element;
  276. {
  277.   char *temp_buf, *temp2;
  278.  
  279.   temp_buf = (char *)malloc(100);
  280.  
  281.   sprintf(temp_buf, "%d", p_table[element].atomic_number);
  282.   XDrawString(p_disp, XtWindow(w), theGC, 10, 10 + mfontheight, temp_buf,
  283.         strlen(temp_buf));
  284.  
  285.   sprintf(temp_buf, "%s", p_table[element].symbol);
  286.   temp2 = strchr(temp_buf, ' ');
  287.   *temp2 = 0;
  288.   XDrawString(p_disp, XtWindow(w), BigGC,
  289.           100 - XTextWidth(BigFont, temp_buf, strlen(temp_buf)) / 2,
  290.           110 + mfontheight, temp_buf, strlen(temp_buf));
  291.  
  292.   sprintf(temp_buf, "%1.4lf", p_table[element].atomic_mass);
  293.   XDrawString(p_disp, XtWindow(w), theGC,
  294.           190 - XTextWidth(mfontstruct, temp_buf, strlen(temp_buf)),
  295.           10 + mfontheight, temp_buf, strlen(temp_buf));
  296.  
  297.   if (p_table[element].boiling_pt > 0.0)
  298.     sprintf(temp_buf, "%.2lf", p_table[element].boiling_pt);
  299.   else
  300.     strcpy(temp_buf, "--");
  301.   XDrawString(p_disp, XtWindow(w), theGC, 10, 60 + mfontheight, temp_buf,
  302.           strlen(temp_buf));
  303.  
  304.   if (p_table[element].melting_pt > 0.0)
  305.     sprintf(temp_buf, "%.2lf", p_table[element].melting_pt);
  306.   else
  307.     strcpy(temp_buf, "--");
  308.   XDrawString(p_disp, XtWindow(w), theGC, 10, 100 + mfontheight, temp_buf,
  309.           strlen(temp_buf));
  310.  
  311.  
  312.   if (p_table[element].density > 0.0)
  313.     sprintf(temp_buf, "%.2lf", p_table[element].density);
  314.   else
  315.     strcpy(temp_buf, "--");
  316.   XDrawString(p_disp, XtWindow(w), theGC, 10, 140 + mfontheight, temp_buf,
  317.           strlen(temp_buf));
  318.  
  319.   if (p_table[element].atomic_radius > 0.0)
  320.     sprintf(temp_buf, "%.2lf", p_table[element].atomic_radius);
  321.   else
  322.     strcpy(temp_buf, "--");
  323.   XDrawString(p_disp, XtWindow(w), theGC,
  324.           190 - XTextWidth(mfontstruct, temp_buf, strlen(temp_buf)),
  325.           60 + mfontheight, temp_buf, strlen(temp_buf));
  326.  
  327.   if (p_table[element].covalent_radius > 0.0)
  328.     sprintf(temp_buf, "%.2lf", p_table[element].covalent_radius);
  329.   else
  330.     strcpy(temp_buf, "--");
  331.   XDrawString(p_disp, XtWindow(w), theGC,
  332.           190 - XTextWidth(mfontstruct, temp_buf, strlen(temp_buf)),
  333.           100 + mfontheight, temp_buf, strlen(temp_buf));
  334.  
  335.   if (p_table[element].atomic_volume > 0.0)
  336.     sprintf(temp_buf, "%.2lf", p_table[element].atomic_volume);
  337.   else
  338.     strcpy(temp_buf, "--");
  339.   XDrawString(p_disp, XtWindow(w), theGC,
  340.           190 - XTextWidth(mfontstruct, temp_buf, strlen(temp_buf)),
  341.           140 + mfontheight, temp_buf, strlen(temp_buf));
  342.  
  343.   strcpy(temp_buf, p_table[element].name);
  344.   temp2 = strchr(temp_buf, ' ');
  345.   *temp2 = 0;
  346.   XDrawString(p_disp, XtWindow(w), theGC,
  347.           100 - XTextWidth(mfontstruct, temp_buf, strlen(temp_buf)) / 2,
  348.           190 + mfontheight, temp_buf, strlen(temp_buf));
  349.  
  350.   free(temp_buf);
  351. }
  352.